import api from './index'
import store from '../store/index'


const db = wx.cloud.database()
const _ = db.command
const $ = db.command.aggregate
const app = getApp()


const publish = async(params)=>{
  var avatarUrl = store.data.user.avatarUrl
  var nickName = store.data.user.nickName
  return new Promise((resolve,reject)=>{
    wx.cloud.callFunction({
      name:'player',
      data:{action:'publish',data:{...params,avatarUrl,nickName}}
    }).then(res=>{
      resolve(res)
    }).catch(err=>{
      reject(err)
    })
  })
 
}


const edit = async(params)=>{
  var id = params.id
  return new Promise((reslove,reject)=>{
    wx.cloud.callFunction({
      name:'player',
      data:{
        action:'edit',data:{...params.team},id
      }
    }).then(res=>{
      reslove(res)
    })
    .catch(err=>{
      reject(err)
    })
  })
}


const del = async(params)=>{
  var {id} = params
  console.log(id)
  return new Promise((reslove,reject)=>{
    wx.cloud.callFunction({
      name:'player',
      data:{action:'del',id}
    }).then(res=>{
      reslove(res)
    })
    .catch(err=>{
      reject(err)
    })
  })
}


const list = async(params)=>{
  var openid = store.data.openid
  return new Promise((reslove,reject)=>{
    wx.cloud.callFunction({
      name:'player',
      data:{action:'list',...params,openid}
    }).then(res=>{
      reslove(res)
    })
    .catch(err=>{
      reject(err)
    })
  })
}


const quit = async(params)=>{
  var {teamID} = params
  console.log(teamID)
  return new Promise((reslove,reject)=>{
    wx.cloud.callFunction({
      name:'player',
      data:{action:'quit',teamID:teamID}
    }).then(res=>{
      reslove(res)
    })
    .catch(err=>{
      reject(err)
    }) 
  })
}


const join = async(params)=>{
  var avatarUrl = store.data.user.avatarUrl
  var nickName = store.data.user.nickName
  var openid = store.data.openid


  return new Promise((reslove,reject)=>{
    wx.cloud.callFunction({
      name:'player',
      data:{action:'join',data:{...params,avatarUrl,nickName,openid}}
    }).then(res=>{
      reslove(res)
    })
    .catch(err=>{
      reject(err)
    }) 
  })
}
const apply = async(params)=>{
  console.log(params) //avatarUrl,nickName,openid,
  
return new Promise((reslove,reject)=>{
  api.team.getTeamMates(params).then(res=>{
    console.log(res.data.mates)
    var mates = res.data.mates
    var index = mates.findIndex(item=>{return item.openid == params.openid})
    console.log(index)
    if(index==-1){
      var leaderID = store.data.openid
      var initiator = 1
      console.log("进入这里")
        wx.cloud.callFunction({
          name:'team',
          data:{action:'apply',data:{...params,leaderID,initiator}}
        }).then(res=>{
          console.log(res.result)
          reslove(res.result)
        })
        .catch(err=>{
          reject(err)
        }) 
    }else{
      reslove({code:"300",massage:"该成员已在队列中"})
    }
  })
})
  
}
const accept = async(params)=>{
  
  var openid = store.data.openid

  return new Promise((reslove,reject)=>{
    wx.cloud.callFunction({
      name:'player',
      data:{action:'accept',data:{...params,openid}}
    }).then(res=>{
      reslove(res)
    })
    .catch(err=>{
      reject(err)
    }) 
  })
}






export default {
  publish,
  edit,
  del,
  join,
  list,
  quit,
  apply,
  accept
}